<?php
session_start();

if(!isset($_SESSION['messages'])){
	$_SESSION['messages']=array();
}

$f=fopen("log.txt","w+");
function logg($t){
	global $f;
	fwrite($f, $t." \n" );
}

logg(date("H:m:s"));

include("inc/config/general.php");
include("inc/comm/db.php");
include("inc/includes/db_functions.php");
include("inc/includes/functions.php");
include("inc/includes/image_functions.php");

include("inc/secure/manager.php");

/* VISTAS con los modelos que deben cargar*/
$allowed_views=array();
$allowed_views['index'] = array( 'view'=>"app/views/index.php" ,'models'=> array('Proyect') );
$allowed_views['newproyect'] = array( 'view'=>"app/views/proyects/new.php",'models'=>array() );
$allowed_views['newwireframe'] = array( 'view'=>"app/views/proyects/newwireframe.php",'models'=>array('Proyect') );

$allowed_views['addscreen'] = array( 'view'=>"app/views/proyects/newscreen.php",'models'=>array('Proyect' , 'Wireframe') );
$allowed_views['manage'] = array( 'view'=>"app/views/proyects/manage.php",'models'=>array('Proyect','Wireframe','Screen') );
$allowed_views['screen'] = array( 'view'=>"app/views/proyects/screenControl.php",'models'=>array('Proyect','Wireframe','Screen','Note','Link') );
$allowed_views['manageusers'] = array( 'view'=>"app/views/users/list.php",'models'=>array('User') );
$allowed_views['newuser'] = array( 'view'=>"app/views/users/new.php",'models'=>array() );
$allowed_views['edituser'] = array( 'view'=>"app/views/users/edit.php",'models'=>array('User','Proyect') );


$allowed_process=array();
$allowed_process['newproyect'] = "Proyect";
$allowed_process['newwireframe'] = "Wireframe";
$allowed_process['newscreen'] = "Screen";
$allowed_process['addnote'] = "Note";
$allowed_process['savelink'] = "Link";
$allowed_process['saveuser'] = "User";
$allowed_process['updateuser'] = "User";


if( isset($_POST['dbprocess']) && array_key_exists($_POST['dbprocess'], $allowed_process)){
	
	$dbprocess=$_POST['dbprocess'];
	logg("db: ".$dbprocess);
	include_once("app/models/M".$allowed_process[$_POST['dbprocess']].".php");
	include_once("app/controllers/C".$allowed_process[$_POST['dbprocess']].".php");
}

if( isset($_GET['v']) && array_key_exists($_GET['v'], $allowed_views) ){
	$key=$_GET['v'];
}else{
	$key='index';
}

for($m=0; $m < count($allowed_views[$key]['models']) ; $m++){
	include_once( "app/models/M".$allowed_views[$key]['models'][$m].".php" );
}

$view=$allowed_views[$key]['view'];

header('Content-type: text/html; charset="utf-8"',true);
include($view);
fclose($f);
?>
